package com.color.admin.system.controller;

import com.color.admin.auth.entity.LoginDto;
import com.color.admin.common.core.base.BaseController;
import com.color.admin.common.core.constant.Constant;
import com.color.admin.common.core.page.PageRequest;
import com.color.admin.common.core.result.ResultData;
import com.color.admin.common.logger.annotation.Log;
import com.color.admin.system.entity.BaseEntity;
import com.color.admin.system.entity.SysUser;
import com.color.admin.system.entity.dto.PwdDto;
import com.color.admin.system.entity.dto.RestPwd;
import com.color.admin.system.entity.dto.UserDto;
import com.color.admin.system.entity.query.UserQueryCriteria;
import com.color.admin.system.servie.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author HaiQiu
 * @date 2021/12/3
 */
@Tag(name = "user-controller", description = "用户相关接口操作")
@RestController
@RequestMapping("/system/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Log("登录接口(内部feign调用)")
    @Operation(description = "登录接口(内部feign调用)", hidden = true)
    @PostMapping("/login")
    public ResultData login(@Parameter(description = "内部服务调用登录方法")
                            @RequestBody @Validated LoginDto loginDto) {
        return decide("登录成功", userService.login(loginDto));
    }


    @Log("退出登录(内部feign调用)")
    @Operation(description = "退出登录(内部feign调用)", hidden = true)
    @PostMapping("/login/out")
    public ResultData loginOut() {
        userService.loginOut();
        return decide("退出成功");
    }

    @Log("用户新增")
    @Operation(summary = "用户新增", description = "用户新增")
    @PostMapping
    public ResultData save(@Parameter(description = "用户新增无需传ID")
                           @RequestBody @Validated(BaseEntity.add.class) SysUser sysUser) {
        userService.save(sysUser);
        return decide(Constant.INSERT);
    }

    @Log("用户修改")
    @Operation(summary = "用户修改", description = "用户修改")
    @PutMapping
    public ResultData update(@Parameter(description = "用户修改需要传入ID")
                             @RequestBody @Validated(BaseEntity.update.class) SysUser sysUser) {
        userService.update(sysUser);
        return decide(Constant.UPDATE);
    }

    @Log("个人中心修改")
    @Operation(summary = "个人中心修改", description = "个人中心修改")
    @PutMapping("/info")
    public ResultData updateInfo(@Parameter(description = "用户修改需要传入ID")
                             @RequestBody SysUser sysUser) {
        userService.updateInfo(sysUser);
        return decide(Constant.UPDATE);
    }

    @Log("批量用户关联修改")
    @Operation(summary = "批量用户关联修改", description = "批量用户关联修改，主要修改用户绑定的部门，角色，岗位等信息修改")
    @PutMapping("/batch")
    public ResultData updateBatch(@Parameter(description = "用户修改需要传入ID")
                                     @RequestBody UserDto userDto) {
        userService.updateBatch(userDto);
        return decide(Constant.UPDATE);
    }

    @Log("修改密码")
    @Operation(summary = "修改密码", description = "用户修改密码")
    @PutMapping("/password")
    public ResultData password(@RequestBody PwdDto pwdDto) {
        userService.password(pwdDto);
        return decide(Constant.UPDATE);
    }

    @Log("重置密码")
    @Operation(summary = "重置密码", description = "用户重置密码")
    @PutMapping("/reset")
    public ResultData restPwd(@RequestBody List<RestPwd> restPwds) {
        userService.restPwd(restPwds);
        return decide(Constant.RESET);
    }

    @Log("用户删除")
    @Operation(summary = "用户删除", description = "批量删除多个用户操作")
    @DeleteMapping
    public ResultData delete(@Parameter(description = "用户ID数组") @RequestBody Set<String> ids) {
        userService.delete(ids);
        return decide(Constant.DELETE);
    }

    @Log("查询用户")
    @Operation(summary = "查询用户", description = "根据用户ID查询用户信息")
    @GetMapping
    public ResultData get(@Parameter(description = "用户ID") @RequestParam String userId) {
        return decide(userService.get(userId));
    }

    @Log("所有用户")
    @Parameters(value = {
            @Parameter(name = "typeId", description = "可选参数：类型ID，可查询deptId，roleId，postId"),
            @Parameter(name = "typeId", description = "可选参数：类型，0部门，1角色，2岗位"),
            @Parameter(name = "pageNum", description = "可选参数：页数，不传默认查询全部"),
            @Parameter(name = "pageSize", description = "可选参数：每页大小，不传默认查询全部")
    })
    @Operation(summary = "所有用户")
    @GetMapping("/userAll")
    public ResultData userAll(@RequestParam(required = false) String typeId,
                              @RequestParam(required = false) Integer type,
                              @RequestParam(required = false) Integer pageNum,
                              @RequestParam(required = false) Integer pageSize) {
        return decide(userService.userAll(typeId,type,pageNum,pageSize));
    }

    @Log("分页模糊查询用户")
    @Operation(summary = "分页查询", description = "分页模糊查询用户信息")
    @PostMapping("/list")
    public ResultData list(@RequestBody PageRequest<UserQueryCriteria> request) {
        return decide(userService.list(request));
    }

}
